{% extends "base.html" %}
{% block extra_css %}
<style>
    /* 房间列表页面特定样式 */
    .room-container {
        display: grid;
        grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
        gap: 1.5rem;
        margin-top: 1.5rem;
    }

    .section-title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin: 2.5rem 0 1.5rem;
        padding-bottom: 0.75rem;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    }

    .section-title h2 {
        font-size: 1.5rem;
        color: var(--accent);
    }

    .action-buttons {
        display: flex;
        gap: 1rem;
        margin-bottom: 1.5rem;
    }

    .btn {
        display: inline-block;
        padding: 0.75rem 1.5rem;
        background: var(--primary);
        color: white;
        text-decoration: none;
        border-radius: 30px;
        font-weight: 600;
        transition: var(--transition);
        border: none;
        cursor: pointer;
        text-align: center;
    }

    .btn:hover {
        background: var(--secondary);
        transform: translateY(-3px);
        box-shadow: 0 5px 15px rgba(67, 97, 238, 0.3);
    }

    .btn-outline {
        background: transparent;
        border: 2px solid var(--primary);
        color: var(--primary);
    }

    .btn-outline:hover {
        background: var(--primary);
        color: white;
    }

    .btn-warning {
        background: var(--warning);
    }

    .btn-warning:hover {
        background: #e01e6b;
    }

    .room-card {
        background: var(--card-bg);
        border-radius: var(--border-radius);
        padding: 1.5rem;
        transition: var(--transition);
        border: 1px solid rgba(255, 255, 255, 0.1);
        height: 100%;
        display: flex;
        flex-direction: column;
    }

    .room-card:hover {
        transform: translateY(-5px);
        box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
        border-color: var(--accent);
    }

    .room-header {
        display: flex;
        justify-content: space-between;
        margin-bottom: 1rem;
    }

    .room-name {
        font-size: 1.25rem;
        font-weight: 600;
        color: white;
        text-decoration: none;
        flex-grow: 1;
    }

    .room-name:hover {
        color: var(--accent);
    }

    .room-stats {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        background: rgba(67, 97, 238, 0.15);
        padding: 0.25rem 0.75rem;
        border-radius: 20px;
        font-size: 0.85rem;
    }

    .room-meta {
        color: var(--gray);
        font-size: 0.9rem;
        margin-top: 0.5rem;
    }

    .room-actions {
        margin-top: auto;
        padding-top: 1rem;
        display: flex;
        gap: 0.75rem;
    }

    .btn-small {
        padding: 0.4rem 0.8rem;
        font-size: 0.85rem;
        border-radius: 20px;
    }

    .empty-message {
        text-align: center;
        padding: 2rem;
        background: rgba(255, 255, 255, 0.03);
        border-radius: var(--border-radius);
        color: var(--gray);
        grid-column: 1 / -1;
    }

    .notice-container {
        position: fixed;
        bottom: 1rem;
        right: 1rem;
        max-width: 350px;
        z-index: 1000;
    }

    #noticeBox {
        background: var(--card-bg);
        border: 1px solid var(--accent);
        border-radius: var(--border-radius);
        padding: 1rem;
        max-height: 300px;
        overflow-y: auto;
        box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    }

    #noticeBox p {
        margin: 0;
        font-weight: 500;
        color: var(--accent);
        padding-bottom: 0.5rem;
        border-bottom: 1px solid rgba(255, 255, 255, 0.1);
        margin-bottom: 0.5rem;
    }

    #noticeBox .notice-item {
        background: rgba(72, 149, 239, 0.1);
        padding: 0.75rem;
        border-radius: 8px;
        margin-bottom: 0.5rem;
        font-size: 0.9rem;
        animation: fadeIn 0.3s ease;
    }

    @keyframes fadeIn {
        from { opacity: 0; transform: translateY(10px); }
        to { opacity: 1; transform: translateY(0); }
    }

    /* 响应式调整 */
    @media (max-width: 768px) {
        .action-buttons {
            flex-direction: column;
        }

        .room-container {
            grid-template-columns: 1fr;
        }

        .section-title {
            flex-direction: column;
            align-items: flex-start;
            gap: 0.5rem;
        }
    }
</style>
{% endblock %}

{% block content %}
<div class="action-buttons">
    <a href="{% url 'PrivateChat:recent_contact' %}" class="btn btn-outline">最近私聊</a>
    <a href="{% url 'User:profile' %}" class="btn btn-outline">个人资料</a>
    <a href="{% url 'Chat:create_room' %}" class="btn">创建新房间</a>
</div>

<div class="section-title">
    <h2>我加入的房间</h2>
</div>
<div class="room-container">
    {% for room in joined_rooms %}
    <div class="room-card">
        <div class="room-header">
            <a href="{% url 'Chat:chat_room' room_id=room.1 %}" class="room-name">{{ room.0 }}</a>
            <div class="room-stats">
                <span>👤 {{room.2}}</span>
            </div>
        </div>
        <div class="room-meta">创建时间: {{room.3}}</div>
    </div>
    {% empty %}
    <div class="empty-message">
        你还没有加入任何房间，去发现一些有趣的房间吧！
    </div>
    {% endfor %}
</div>

<div class="section-title">
    <h2>我创建的房间</h2>
</div>
<div class="room-container">
    {% for room in created_rooms %}
    <div class="room-card">
        <div class="room-header">
            <a href="{% url 'Chat:chat_room' room_id=room.1 %}" class="room-name">{{ room.0 }}</a>
            <div class="room-stats">
                <span>👤 {{room.2}}</span>
            </div>
        </div>
        <div class="room-meta">创建时间: {{room.3}}</div>
        <div class="room-actions">
            <a href="{% url 'Chat:chat_room' room_id=room.1 %}" class="btn btn-small">进入房间</a>
            <a href="{% url 'Chat:manage_room' room_id=room.1 %}" class="btn btn-small btn-outline">管理房间</a>
        </div>
    </div>
    {% empty %}
    <div class="empty-message">
        你还没有创建任何房间，点击上方按钮创建一个吧！
    </div>
    {% endfor %}
</div>

<div class="section-title">
    <h2>所有房间</h2>
</div>
<div class="room-container">
    {% for room in all_rooms %}
    <div class="room-card">
        <div class="room-header">
            <a href="{% url 'Chat:chat_room' room_id=room.1 %}" class="room-name">{{ room.0 }}</a>
            <div class="room-stats">
                <span>👤 {{room.2}}</span>
            </div>
        </div>
        <div class="room-meta">创建时间: {{room.3}}</div>
        <div class="room-actions">
            <a href="{% url 'Chat:chat_room' room_id=room.1 %}" class="btn btn-small">加入房间</a>
        </div>
    </div>
    {% empty %}
    <div class="empty-message">
        当前没有激活房间，成为第一个创建者吧！
    </div>
    {% endfor %}
</div>

<div class="notice-container">
    <div id="noticeBox">
        <p>消息提醒</p>
    </div>
</div>
<div class="notice-container">
    <input id="msg" type="text"  placeholder="输入消息..." autocomplete="off">
    <button type="button" id="send">发送</button>
</div>
<script>
    document.addEventListener('DOMContentLoaded',()=>{
        const webNotice = new WebSocket(`ws://${window.location.host}/ws/privatechat/notice`)
        const noticeBox = document.getElementById('noticeBox')
        webNotice.onopen = () => console.log("聊天提醒已连接")
        webNotice.onmessage = (e) => {
            const notice = JSON.parse(e.data)
            if (notice.type === "notify_func"){
                const noticeItem = document.createElement("div")
                noticeItem.className = "notice-item"
                noticeItem.innerHTML = `${notice.message}`
                noticeBox.appendChild(noticeItem)
                noticeBox.scrollTop = noticeBox.scrollHeight;

                // 自动移除旧消息
                if (noticeBox.children.length > 10) {
                    noticeBox.removeChild(noticeBox.children[1]);
                }
            }
        }
    })
    const ws = new WebSocket(`ws://${window.location.host}/ws/deepseek_chat/`);

    ws.onopen = () => console.log("WebSocket connected");

    ws.onmessage = (event) => {
    const data = JSON.parse(event.data);
    if (data.type === "agent_reply") {
        console.log("智能体回复:", data.reply);
        }
    };

        const sendMessage = () => {
            const msg = document.getElementById('msg')
            let msgs = msg.value.trim();
            if (msgs) {
                ws.send(
                    JSON.stringify({
                        message: msgs,
                    })
                );
                msg.value = "";
            }
        };

        // 点击按钮发送
        document.getElementById('send').addEventListener("click", sendMessage)
</script>
{% endblock %}